﻿using Business;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BVVD_Website
{
    public partial class MasterPageLayout : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (Session["user"] != null)
            {

            }
            else
            {
                Response.Redirect("/frmLogin.aspx");

            }

            //--------------------------
            if (!IsPostBack)
            {

                LST_NSD user = (Session["user"] as LST_NSD);

                BVVDDataContext db = new BVVDDataContext();
                F_NHOM_NSD gr = (from g in db.F_NHOM_NSDs
                                 where g.MANSD.Equals(user.MANSD)
                                 select g).SingleOrDefault();

                var menuLeftList = (from mn in db.LST_MENUs
                                    where mn.TYPE == 2
                                    join g_m in db.V_PHANQUYENs on new { MAMN = mn.MAMN, MANSD = gr.MANSD } equals new { MAMN = g_m.MAMN, MANSD = g_m.MANSD } into temp1
                                    from tmp1 in temp1.DefaultIfEmpty()
                                    select new MenuLeft
                                    {
                                        TEN = mn.TEN,
                                        LEVEL = mn.LEVEL,
                                        TYPE = mn.TYPE,
                                        LINK = mn.LINK,
                                        MAMN = mn.MAMN,
                                        MAMN_CHA = mn.MAMN_CHA,
                                        ORDER = mn.ORDER,
                                        MANHOM = gr.MANHOM,
                                        SHOW = tmp1.SHOW != null ? (tmp1.SHOW.ToString().Equals("1") ? 1 : 0) : 0,                                        
                                        ADD = tmp1.ADD != null ? (tmp1.ADD.ToString().Equals("1") ? 1 : 0) : 0,
                                        EDIT = tmp1.EDIT != null ? (tmp1.EDIT.ToString().Equals("1") ? 1 : 0) : 0,
                                        DELETE = tmp1.DELETE != null ? (tmp1.DELETE.ToString().Equals("1") ? 1 : 0) : 0
                                    }).ToList();


                foreach (MenuLeft mn in menuLeftList.Where(a => a.LEVEL == 0))
                {
                    RadPanelItem rpi = null;
                    if (mn.SHOW != 0)
                    {
                        rpi = new RadPanelItem(mn.TEN, mn.LINK);

                    }
                    else
                    {
                        rpi = new RadPanelItem(mn.TEN, "#");
                        //rpi.ForeColor = Color.Gray;
                    }

                    loadSubMenu(rpi, mn.MAMN, gr);
                    RadPanelBar1.Items.Add(rpi);
                }

                var menuTopList = (from mt in db.LST_MENUs
                                   where mt.TYPE == 1
                                   select mt).ToList().OrderBy(a => a.ORDER);
                foreach (LST_MENU mt in menuTopList)
                {
                    RadMenuItem item = new RadMenuItem(mt.TEN.Trim(), mt.LINK);
                    if (mt.TEN.Equals("Welcome"))
                    {
                        item.Value = "Welcome";
                        item.ForeColor = Color.Green;
                        RadMenuItem spare = new RadMenuItem();
                        spare.IsSeparator = true;
                        RadMenu1.Items.Add(spare);
                    }
                    if (mt.TEN.Equals("Thoát"))
                    {
                        item.ForeColor = Color.Red;



                    }
                    RadMenu1.Items.Add(item);

                }
                RadMenuItem rmn = RadMenu1.Items.FindItemByValue("Welcome");
                rmn.Text = (Session["user"] as LST_NSD).TENNSD;
            }




        }


        private void loadSubMenu(RadPanelItem pItem, string mamenu, F_NHOM_NSD gr)
        {
            BVVDDataContext db = new BVVDDataContext();
            LST_NSD user = (Session["user"] as LST_NSD);
            var list = (from mn in db.LST_MENUs
                        where mn.MAMN_CHA.Equals(mamenu)
                        join g_m in db.V_PHANQUYENs on new { MAMN = mn.MAMN, MANSD = gr.MANSD } equals new { MAMN = g_m.MAMN, MANSD = g_m.MANSD } into temp1
                        from tmp1 in temp1.DefaultIfEmpty()
                        select new MenuLeft
                        {
                            TEN = mn.TEN,
                            LEVEL = mn.LEVEL,
                            TYPE = mn.TYPE,
                            LINK = mn.LINK,
                            MAMN = mn.MAMN,
                            MAMN_CHA = mn.MAMN_CHA,
                            ORDER = mn.ORDER,
                            MANHOM = gr.MANHOM,
                            SHOW = tmp1.SHOW != null ? (tmp1.SHOW.ToString().Equals("1") ? 1 : 0) : 0,
                            ADD = tmp1.ADD != null ? (tmp1.ADD.ToString().Equals("1") ? 1 : 0) : 0,
                            EDIT = tmp1.EDIT != null ? (tmp1.EDIT.ToString().Equals("1") ? 1 : 0) : 0,
                            DELETE = tmp1.DELETE != null ? (tmp1.DELETE.ToString().Equals("1") ? 1 : 0) : 0
                        }).ToList();
            foreach (MenuLeft mn in list.OrderBy(a => a.ORDER))
            {
                RadPanelItem rpi = null;
                if (mn.SHOW != 0)
                {
                    rpi = new RadPanelItem(mn.TEN, mn.LINK);
                    
                }
                else
                {
                    rpi = new RadPanelItem(mn.TEN, "#");
                    if (mn.LINK!=null)
                    {
                        rpi.ForeColor = Color.Gray;
                        rpi.Visible = false;
                    }
                   

                }
                loadSubMenu(rpi, mn.MAMN, gr);
                pItem.Items.Add(rpi);
            }


        }
    }

    public class MenuLeft
    {
        public string TEN { get; set; }
        public int? LEVEL { get; set; }
        public int? TYPE { get; set; }
        public string LINK { get; set; }
        public string MAMN { get; set; }
        public string MAMN_CHA { get; set; }
        public int? ORDER { get; set; }
        public string MANHOM { get; set; }
        public int SHOW { get; set; }
        public int ADD { get; set; }
        public int EDIT { get; set; }
        public int DELETE { get; set; }


    }
}